package GUI;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import org.eclipse.swt.SWT;

import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;




import DB.DBPowerConnector;

import Entities.Powers;


import com.cloudgarden.resource.SWTResourceManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Manager extends org.eclipse.swt.widgets.Composite {
	private Menu menu1;
	private Text text9;
	private Text text8;
	private Text text7;
	private Text text6;
	private Text text5;
	private Text text4;
	private Text text3;
	private Text text2;
	private Text text1;
	private Button displayPowers;
	private Button importButton;
	private Text p1;
	private MenuItem exitMenuItem;
	private MenuItem closeFileMenuItem;
	private MenuItem saveFileMenuItem;
	private MenuItem newFileMenuItem;
	private MenuItem openFileMenuItem;
	private Menu fileMenu;
	private MenuItem fileMenuItem;
	private Label strength;
	private Label wisdom;
	private Button back;
	private Button next;
	private Group group1;
	private Group group2;
	private SashForm sashForm1;
	private Button save;
	private Text wisdomText;
	private Text durabilityText;
	private Text strengthText;
	private Label durability;
	
	private int page=0;
	public static final int resultsPerPage=10;
	private Entities.Powers[] powers= null;
	private int selectedCell=0;
	private Text selectedTextBox;
	private Image image;
	
	
	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	public Manager(Composite parent, int style) {
		super(parent, style);
		initGUI();
	}
	
	/**
	* Initializes the GUI.
	*/
	private void initGUI() {
		try {
			this.setSize(748, 324);
			this.setBackground(SWTResourceManager.getColor(48, 48, 48));
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				FormData sashForm1LData = new FormData();
				sashForm1LData.left =  new FormAttachment(0, 1000, 260);
				sashForm1LData.top =  new FormAttachment(0, 1000, 75);
				sashForm1LData.width = 75;
				sashForm1LData.height = 56;
				sashForm1LData.right =  new FormAttachment(1000, 1000, -428);
				sashForm1LData.bottom =  new FormAttachment(1000, 1000, -194);
				sashForm1 = new SashForm(this, SWT.NONE);
				sashForm1.setLayoutData(sashForm1LData);

			}
			{
				group2 = new Group(this, SWT.NONE);
				FormLayout group2Layout = new FormLayout();
				group2.setLayout(group2Layout);
				FormData group2LData = new FormData();
				group2LData.width = 142;
				group2LData.height = 114;
				group2LData.left =  new FormAttachment(0, 1000, 232);
				group2LData.top =  new FormAttachment(0, 1000, 166);
				group2.setLayoutData(group2LData);
				group2.setText("group2");
				{
					save = new Button(group2, SWT.PUSH | SWT.CENTER);
					FormData saveLData = new FormData();
					saveLData.width = 88;
					saveLData.height = 24;
					saveLData.left =  new FormAttachment(137, 1000, 0);
					saveLData.right =  new FormAttachment(757, 1000, 0);
					saveLData.top =  new FormAttachment(714, 1000, 0);
					saveLData.bottom =  new FormAttachment(925, 1000, 0);
					save.setLayoutData(saveLData);
					save.setText("Save Changes");
					save.addMouseListener(new MouseAdapter() {
						public void mouseDown(MouseEvent evt) {
							powers[selectedCell].setStrength(Double.parseDouble(strengthText.getText()));
							powers[selectedCell].setDurability(Double.parseDouble(durabilityText.getText()));
							powers[selectedCell].setWisdom(Double.parseDouble(wisdomText.getText()));
							
							try {
								GUIThreadPool.pool.submit(new Runnable(){
									public void run(){
									DBPowerConnector.setPower(powers[selectedCell]);
									DBPowerConnector.setChanged(powers[selectedCell].getID(),"character_powers");	
									}
								}).get();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (ExecutionException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							//pool.shutdown();
						}
						
					});
					
				}
				{
					wisdom = new Label(group2, SWT.NONE);
					FormData wisdomLData = new FormData();
					wisdomLData.width = 43;
					wisdomLData.height = 14;
					wisdomLData.left =  new FormAttachment(66, 1000, 0);
					wisdomLData.right =  new FormAttachment(369, 1000, 0);
					wisdomLData.top =  new FormAttachment(442, 1000, 0);
					wisdomLData.bottom =  new FormAttachment(565, 1000, 0);
					wisdom.setLayoutData(wisdomLData);
					wisdom.setText("Wisdom");
				}
				{
					durability = new Label(group2, SWT.NONE);
					FormData durabilityLData = new FormData();
					durabilityLData.width = 49;
					durabilityLData.height = 14;
					durabilityLData.left =  new FormAttachment(66, 1000, 0);
					durabilityLData.right =  new FormAttachment(411, 1000, 0);
					durabilityLData.top =  new FormAttachment(92, 1000, 0);
					durabilityLData.bottom =  new FormAttachment(214, 1000, 0);
					durability.setLayoutData(durabilityLData);
					durability.setText("Durability");
				}
				{
					strength = new Label(group2, SWT.NONE);
					FormData strengthLData = new FormData();
					strengthLData.width = 49;
					strengthLData.height = 14;
					strengthLData.left =  new FormAttachment(66, 1000, 0);
					strengthLData.right =  new FormAttachment(411, 1000, 0);
					strengthLData.top =  new FormAttachment(267, 1000, 0);
					strengthLData.bottom =  new FormAttachment(390, 1000, 0);
					strength.setLayoutData(strengthLData);
					strength.setText("Strength");
				}
				{
					strengthText = new Text(group2, SWT.NONE);
					FormData strengthTextLData = new FormData();
					strengthTextLData.width = 41;
					strengthTextLData.height = 14;
					strengthTextLData.left =  new FormAttachment(545, 1000, 0);
					strengthTextLData.right =  new FormAttachment(876, 1000, 0);
					strengthTextLData.top =  new FormAttachment(267, 1000, 0);
					strengthTextLData.bottom =  new FormAttachment(390, 1000, 0);
					strengthText.setLayoutData(strengthTextLData);
					strengthText.setText(" ");
				}
				{
					durabilityText = new Text(group2, SWT.NONE);
					FormData durabilityTextLData = new FormData();
					durabilityTextLData.width = 41;
					durabilityTextLData.height = 14;
					durabilityTextLData.left =  new FormAttachment(545, 1000, 0);
					durabilityTextLData.right =  new FormAttachment(876, 1000, 0);
					durabilityTextLData.top =  new FormAttachment(92, 1000, 0);
					durabilityTextLData.bottom =  new FormAttachment(214, 1000, 0);
					durabilityText.setLayoutData(durabilityTextLData);
					durabilityText.setText(" ");
				}
				{
					wisdomText = new Text(group2, SWT.NONE);
					FormData wisdomTextLData = new FormData();
					wisdomTextLData.width = 41;
					wisdomTextLData.height = 14;
					wisdomTextLData.left =  new FormAttachment(545, 1000, 0);
					wisdomTextLData.right =  new FormAttachment(876, 1000, 0);
					wisdomTextLData.top =  new FormAttachment(442, 1000, 0);
					wisdomTextLData.bottom =  new FormAttachment(565, 1000, 0);
					wisdomText.setLayoutData(wisdomTextLData);
					wisdomText.setText(" ");
				}
			}
			{
				back = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData backLData = new FormData();
				backLData.left =  new FormAttachment(0, 1000, 98);
				backLData.top =  new FormAttachment(0, 1000, 297);
				backLData.width = 37;
				backLData.height = 24;
				back.setLayoutData(backLData);
				back.setText("Back");
				back.addMouseListener(new MouseAdapter() {
					public void mouseDown(MouseEvent evt) {
						if (page>=resultsPerPage){
							page-=resultsPerPage;
							unselectOtherCells();
							displayPowers();
						}
						if (powers!=null&&powers[resultsPerPage-1]!=null)
							next.setVisible(true);
					}

				});

				
			}
			{
				next = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData nextLData = new FormData();
				nextLData.left =  new FormAttachment(0, 1000, 141);
				nextLData.top =  new FormAttachment(0, 1000, 297);
				nextLData.width = 38;
				nextLData.height = 24;
				next.setLayoutData(nextLData);
				next.setText("Next");
				next.addMouseListener(new MouseAdapter() {
					public void mouseDown(MouseEvent evt) {
						
						page+=10;
						unselectOtherCells();
						displayPowers();
						if (powers[0]==null) next.setVisible(false);
					}

				});

			}
			{
				displayPowers = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData displayPowersLData = new FormData();
				displayPowersLData.left =  new FormAttachment(0, 1000, 35);
				displayPowersLData.top =  new FormAttachment(0, 1000, 33);
				displayPowersLData.width = 92;
				displayPowersLData.height = 24;
				displayPowers.setLayoutData(displayPowersLData);
				displayPowers.setText("Display Powers");
				displayPowers.addMouseListener(new MouseAdapter() {
					public void mouseDown(MouseEvent evt) {
						displayPowers();
					}

				});
			}
			
			{
				importButton = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData importButtonLData = new FormData();
				importButtonLData.left =  new FormAttachment(0, 1000, 657);
				importButtonLData.top =  new FormAttachment(0, 1000, 19);
				importButtonLData.width = 73;
				importButtonLData.height = 33;
				importButton.setLayoutData(importButtonLData);
				importButton.setText("IMPORT");
				importButton.addMouseListener(new MouseAdapter() {
					public void mouseDown(MouseEvent evt) {
						importButtonMouseDown(evt);
					}
				});
			}
			{
				group1 = new Group(this, SWT.NONE);
				GridLayout group1Layout = new GridLayout();
				group1Layout.makeColumnsEqualWidth = true;
				group1.setLayout(group1Layout);
				FormData group1LData = new FormData();
				group1LData.width = 167;
				group1LData.height = 212;
				group1LData.left =  new FormAttachment(0, 1000, 35);
				group1LData.top =  new FormAttachment(0, 1000, 63);
				group1.setLayoutData(group1LData);
				group1.setText("powers");
				{
					p1 = new Text(group1, SWT.MULTI | SWT.WRAP);
					GridData p1LData = new GridData();
					p1LData.widthHint = 150;
					p1LData.heightHint = 14;
					p1.setLayoutData(p1LData);
					if (powers!=null) p1.setText(powers[0].getName());
					else p1.setText(" ");
					p1.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(p1,0);
						}
					});
				}
				{
					text3 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text3.setText(powers[1].getName());
					else text3.setText(" ");
					GridData text3LData = new GridData();
					text3LData.widthHint = 150;
					text3LData.heightHint = 14;
					text3.setLayoutData(text3LData);
					text3.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text3,1);
						}
					});
				}
				{
					text1 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null)
						text1.setText(powers[2].getName());
					else text1.setText(" ");
					GridData text1LData = new GridData();
					text1LData.widthHint = 150;
					text1LData.heightHint = 14;
					text1.setLayoutData(text1LData);
					text1.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text1,2);
						}
					});
				}
				{
					text4 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text4.setText(powers[3].getName());
					else text4.setText(" ");
					GridData text4LData = new GridData();
					text4LData.widthHint = 150;
					text4LData.heightHint = 14;
					text4.setLayoutData(text4LData);
					text4.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text4,3);
						}
					});
				}
				{
					text5 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text5.setText(powers[4].getName());
					else text5.setText(" ");
					GridData text5LData = new GridData();
					text5LData.widthHint = 150;
					text5LData.heightHint = 14;
					text5.setLayoutData(text5LData);
					text5.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text5,4);		}
					});
				}
				{
					text2 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text2.setText(powers[5].getName());
					else text2.setText(" ");
					GridData text2LData = new GridData();
					text2LData.widthHint = 150;
					text2LData.heightHint = 14;
					text2.setLayoutData(text2LData);
					text2.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text2,5);
						}
					});
				}
				{
					text8 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text8.setText(powers[6].getName());
					else text8.setText(" ");
					GridData text8LData = new GridData();
					text8LData.widthHint = 150;
					text8LData.heightHint = 14;
					text8.setLayoutData(text8LData);
					text8.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text8,6);
						}
					});
				}
				{
					text7 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text7.setText(powers[7].getName());
					else text7.setText(" ");
					GridData text7LData = new GridData();
					text7LData.widthHint = 150;
					text7LData.heightHint = 14;
					text7.setLayoutData(text7LData);
					text7.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text7,7);
						}
					});
				}
				{
					text6 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text6.setText(powers[8].getName());
					else text6.setText(" ");
					GridData text6LData = new GridData();
					text6LData.widthHint = 150;
					text6LData.heightHint = 14;
					text6.setLayoutData(text6LData);
					text6.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text6,8);
						}
					});
				}
				{
					text9 = new Text(group1, SWT.MULTI | SWT.WRAP);
					if (powers!=null) text9.setText(powers[9].getName());
					else text9.setText(" ");
					GridData text9LData = new GridData();
					text9LData.widthHint = 150;
					text9LData.heightHint = 14;
					text9.setLayoutData(text9LData);
					text9.addMouseListener(new MouseAdapter(){
						public void mouseDown(MouseEvent evt){
							textBoxHandler(text9,9);
						}
					});
				}
			}
			{
				menu1 = new Menu(getShell(), SWT.BAR);
				getShell().setMenuBar(menu1);
				{
					fileMenuItem = new MenuItem(menu1, SWT.CASCADE);
					fileMenuItem.setText("File");
					{
						fileMenu = new Menu(fileMenuItem);
						{
							openFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
							openFileMenuItem.setText("Open");
						}
						{
							newFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
							newFileMenuItem.setText("New");
						}
						{
							saveFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
							saveFileMenuItem.setText("Save");
						}
						{
							closeFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
							closeFileMenuItem.setText("Close");
						}
						{
							exitMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
							exitMenuItem.setText("Exit");
						}
						fileMenuItem.setMenu(fileMenu);
					}
				}
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void importButtonMouseDown(MouseEvent evt) {
		Shell shell2 = new Shell(getDisplay());
		Importer inst = new Importer(shell2, SWT.NULL);
		Point size = inst.getSize();
		shell2.setLayout(new FillLayout());
		shell2.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell2.pack();
		} else {
			Rectangle shellBounds = shell2.computeTrim(0, 0, size.x, size.y);
			shell2.setSize(shellBounds.width, shellBounds.height);
		}
		shell2.open();
		while (!shell2.isDisposed()) {
			if (!getDisplay().readAndDispatch())
				getDisplay().sleep();
		}
	}
	//private void putImage(Image img){
		//this.image=img;
	//}
	private void textBoxHandler(Text text ,int numOfCell){
		selectedCell=numOfCell;
		selectedTextBox=text;
		unselectOtherCells();
		selectedTextBox.setBackground(SWTResourceManager.getColor(0, 255, 255));
		if (powers!=null&&powers[selectedCell]!=null){
			strengthText.setText(String.valueOf(powers[selectedCell].getStrength()));
			durabilityText.setText(String.valueOf(powers[selectedCell].getDurability()));
			wisdomText.setText(String.valueOf(powers[selectedCell].getWisdom()));
		
			try {
				image = GUIThreadPool.pool.submit(new Callable<Image>(){
					public Image call(){
					String freeBaseId = DBPowerConnector.getfreeBaseID(powers[selectedCell].getID(),"character_powers");
					return  new Image(Manager.super.getDisplay(),DBPowerConnector.getInputStreamOfPicture(freeBaseId));
					//Display.getDefault().asyncExec(new Runnable(){
				      //  public void run(){
				        //	putImage(img);
				       // }});
				}	
			}).get();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		sashForm1.setBackgroundImage(image);
		}
		
	}
	//private void putData(Powers[] pwrs){
		//this.powers=pwrs.clone();
	//}
	protected void displayPowers(){
		try {
			powers= GUIThreadPool.pool.submit(new Callable<Entities.Powers[]>(){
				public Powers[] call(){
				return DBPowerConnector.getPowers(page);
				//Display.getDefault().syncExec(new Runnable(){
			      //  public void run(){
			        //	putData(pwrs);
			        //}});
			}}).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//pool.shutdown();
		if (powers!=null){
			if (powers[0]!=null)p1.setText(powers[0].getName()); else p1.setText("");
			if (powers[1]!=null) text3.setText(powers[1].getName()); else text3.setText("");
			if (powers[2]!=null) text1.setText(powers[2].getName());else text1.setText("");
			if (powers[3]!=null) text5.setText(powers[3].getName()); else text5.setText("");
			if (powers[4]!=null) text4.setText(powers[4].getName());else text4.setText("");
			if (powers[5]!=null) text2.setText(powers[5].getName()); else text2.setText("");
			if (powers[6]!=null) text8.setText(powers[6].getName()); else text8.setText("");
			if (powers[7]!=null) text7.setText(powers[7].getName()); else text7.setText("");
			if (powers[8]!=null) text6.setText(powers[8].getName()); else text6.setText("");
			if (powers[9]!=null) text9.setText(powers[9].getName()); else text9.setText("");
		}
	}

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		Manager inst = new Manager(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		GUIThreadPool.terminatePool();
	}
	
	public Button getDisplayPowers() {
		return displayPowers;
	}
	private void unselectOtherCells(){
		text3.setBackground(SWTResourceManager.getColor(255, 255, 255));
		p1.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text1.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text5.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text4.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text2.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text8.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text7.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text6.setBackground(SWTResourceManager.getColor(255, 255, 255));
		text9.setBackground(SWTResourceManager.getColor(255, 255, 255));	
	}
	
	
}